# -*- coding: utf-8 -*-
import scrapy
from area.items import ShengShiQuItem

class SsqSpider(scrapy.Spider):
    name = 'ssq'
    allowed_domains = ['www.stats.gov.cn']
    start_urls = ['http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/index.html']

    def parse(self, response):
        item = ShengShiQuItem()
        id = 10
        for sel in response.xpath('//tr[@class="provincetr"]/td'):
            item['id'] = id
            item['pid'] = ""
            item['name'] = sel.xpath('a/text()').extract_first()
            link =  sel.xpath('a/@href').extract_first()
            id = id + 1
            url = response.urljoin(link)
            yield scrapy.Request(url, self.getcity, meta={'pid': id})
            yield item
            
    """
    获取市
    """
    def getcity(self, response):
        item = ShengShiQuItem()
        for sel in response.xpath('//tr[@class="citytr"]'):
            item['id'] = sel.xpath('td[1]/a/text()').extract_first()
            item['pid'] = response.meta['pid']
            item['name'] = sel.xpath('td[last()]/a/text()').extract_first()
            link = sel.xpath('td[last()]/a/@href').extract_first()
            url = response.urljoin(link)
            yield scrapy.Request(url, self.getcounty, meta={'pid': item['id']})
            yield item

    """
    获取县
    """  
    def getcounty(self, response):
        item = ShengShiQuItem()
        for sel in response.xpath('//tr[@class="countytr"]'):
            item['id'] = sel.xpath('td[1]/a/text()').extract_first()
            item['pid'] = response.meta['pid']
            item['name'] = sel.xpath('td[last()]/a/text()').extract_first()
            link = sel.xpath('td[last()]/a/@href').extract_first()
            url = response.urljoin(link)
            yield scrapy.Request(url, self.gettown, meta={'pid': item['id']})
            yield item

    """
    获取镇
    """
    def gettown(self, response):
        item = ShengShiQuItem()
        for sel in response.xpath('//tr[@class="towntr"]'):
            item['id'] = sel.xpath('td[1]/a/text()').extract_first()
            item['pid'] = response.meta['pid']
            item['name'] = sel.xpath('td[last()]/a/text()').extract_first()
            link = sel.xpath('td[last()]/a/@href').extract_first()
            url = response.urljoin(link)
            yield scrapy.Request(url, self.gettown, meta={'pid': item['id']})
            yield item
    """
    获取村
    """
    def getvillage(self, response):
        item = ShengShiQuItem()
        for sel in response.xpath('//tr[@class="villagetr"]'):
            item['id'] = sel.xpath('td[1]/a/text()').extract_first()
            item['pid'] = response.meta['pid']
            item['name'] = sel.xpath('td[last()]/a/text()').extract_first()
            yield item